﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DropHandler.cs" company="Jens Horstmann">
//   Copyright (c) Jens Horstmann. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace MiniMusicPlayer.Classes
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Windows;

    /// <summary>
    /// The drop handler.
    /// </summary>
    public class DropHandler
    {
        /// <summary>
        /// The music files.
        /// </summary>
        private readonly string[] musicFiles = { ".mp3" };

        /// <summary>
        /// Initializes a new instance of the <see cref="DropHandler"/> class.
        /// </summary>
        /// <param name="playlist">
        /// The playlist.
        /// </param>
        public DropHandler(Playlist playlist)
        {
            this.Playlist = playlist;
        }

        /// <summary>
        /// Gets the playlist.
        /// </summary>
        public Playlist Playlist { get; private set; }

        /// <summary>
        /// The handle drop.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        public void HandleDrop(DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var data = e.Data.GetData(DataFormats.FileDrop, true);
                var filePaths = data as string[];
                var validMusicFiles = this.FilterMusicFiles(filePaths);
                if (validMusicFiles.Length > 0)
                {
                    this.Playlist.AddFiles(validMusicFiles);
                }
            }
        }

        /// <summary>
        /// The filter music files.
        /// </summary>
        /// <param name="filePaths">
        /// The file paths.
        /// </param>
        /// <returns>
        /// The <see cref="FileInfo[]"/>.
        /// </returns>
        public FileInfo[] FilterMusicFiles(string[] filePaths)
        {
            var musicFileList = new List<FileInfo>();

            foreach (var fileInfo in filePaths.Select(filePath => new FileInfo(filePath)))
            {
                musicFileList.AddRange(
                    from musicFile in this.musicFiles where fileInfo.Extension == musicFile select fileInfo);
            }

            return musicFileList.ToArray();
        }
    }
}